package com.example.administrator.szb.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.GZBean;
import com.example.administrator.szb.bean.MyGZBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;
import java.util.List;

/**
 * Created by stone on 2018/1/18.
 */

public class GuanzhuAdapter extends android.widget.BaseAdapter {
    private List list;
    private Context context;
    private int type;
    private LayoutInflater inflater;
    public GuanzhuAdapter(Context context,List list, int type) {
        this.list = list;
        this.context = context;
        this.type = type;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public MyGZBean.DataBean getItem(int position) {
        return (MyGZBean.DataBean)list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.view_mygz_item,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        final MyGZBean.DataBean dataBean = getItem(position);
        ((TextView)holder.getView(R.id.textView54)).setText(dataBean.getName());
        if (dataBean.getReal_name()!=null&&dataBean.getReal_name().size()>0&&dataBean.getReal_name().get(0).length()>0){
            //String arr[] = dataBean.getReal_name().split("-");
            ((TextView)holder.getView(R.id.textView54)).setText(dataBean.getName()+"("+dataBean.getReal_name().get(0)+"经理)");
        }
        if (!TextUtils.isEmpty(dataBean.getReal_name2())){
            ((TextView)holder.getView(R.id.textView54)).setText(dataBean.getName()+"("+dataBean.getReal_name2()+")");
        }
        if (type == 2){
            ((CheckBox)holder.getView(R.id.checkBox2)).setVisibility(View.GONE);
        }else {
            final CheckBox checkBox = (CheckBox)holder.getView(R.id.checkBox2);
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b){
                        checkBox.setText("关注");
                        checkBox.setTextColor(context.getResources().getColor(R.color.basecolor2));
                        checkBox.setBackground(context.getResources().getDrawable(R.drawable.border_content2));
                        doGZOrNOt(dataBean.getId());
                    }else{
                        checkBox.setText("取消关注");
                        checkBox.setTextColor(context.getResources().getColor(R.color.gray1));
                        checkBox.setBackground(context.getResources().getDrawable(R.drawable.border_content3));
                        doGZOrNOt(dataBean.getId());
                    }

                }
            });
        }

        if (dataBean.getType() == 2){
            (holder.getView(R.id.textView67)).setVisibility(View.VISIBLE);
            (holder.getView(R.id.textView165)).setVisibility(View.GONE);
        }else{
            (holder.getView(R.id.textView67)).setVisibility(View.GONE);
            (holder.getView(R.id.textView165)).setVisibility(View.VISIBLE);
            ((TextView)holder.getView(R.id.textView165)).setText((String)dataBean.getSketch()+"");
        }

        if (!TextUtils.isEmpty(dataBean.getHeadimg())){
            Glide.with(context).load(dataBean.getHeadimg()).error(R.mipmap.default_header_s).into((ImageView) holder.getView(R.id.imageView26));
        }

        return convertView;
    }

    private void doGZOrNOt(int id) {
        HashMap<String,String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        //int id = SampleApplicationLike.getUserloginInstance().getId();
        map.put("id", SampleApplicationLike.getUserloginInstance().getId()+"");
        map.put("quiz_id",id+"");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, GZBean.class, URLAddress.URLBASE + URLAddress.URL_GZ, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                GZBean gzBean = (GZBean) response;
                if (gzBean.getResult() == 1){
                    Toasts.show(context, ""+gzBean.getData().getMsg(), Toast.LENGTH_SHORT);
                }else{
                    Toasts.show(context, ""+gzBean.getErr_msg(), Toast.LENGTH_SHORT);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
                //Toasts.show(context, ""+error.toString(), Toast.LENGTH_SHORT);
            }
        });
    }

    class ViewHolder{
        View view;

        public ViewHolder(View view) {
            this.view = view;
        }

        public View getView(int id) {
            return view.findViewById(id);
        }
    }
}
